﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Happy.Activities.Domain.WorkflowInstances;

namespace Happy.Activities.Domain.Workflows
{
    /// <summary>
    /// 代表了流程中的一个人工节点。
    /// </summary>
    public class ManualActivity : Activity
    {
        /// <inheritdoc />
        protected override bool CanEnter(WorkflowContext context, ActivityInstance activityInstance)
        {
            return true;
        }

        /// <inheritdoc />
        protected override void OnEnter(WorkflowContext context, ActivityInstance activityInstance)
        {

        }

        /// <inheritdoc />
        protected override bool CanExit(WorkflowContext context, ActivityInstance activityInstance)
        {
            return true;
        }

        /// <inheritdoc />
        protected override void OnExit(WorkflowContext context, ActivityInstance activityInstance)
        {
            var routers = context
                .WorkflowInstance
                .Workflow
                .GetRoutersByFromId(this.Id);

            foreach (var router in routers)
            {
                //遇到第一个能执行的路由，进执行路由。
                if (router.GetCondition().CanRoute(context, activityInstance, router))
                {
                    router.Route(context, activityInstance);

                    break;
                }
            }
        }
    }
}
